package com.ym.manager.admin.group;

import com.alibaba.fastjson.JSONObject;
import com.ym.datasource.redis.JedisClient;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.*;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 但何非
 * @Date 2022/11/12 21:10
 * @ClassName: GroupController
 * @Description:微信群管理
 * @Version:
 */
@Controller
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private JedisClient jedisClient;
    @Autowired
    private GroupService groupService;
    @RequestMapping("/index")
    public String index(Model model){
        return "group/list";
    }

    @ResponseBody
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "0") int start,
                       @RequestParam(defaultValue = "10")int length,
                       @RequestParam(defaultValue = "")String groupName,
                       @RequestParam(defaultValue = "0")int id,
                       @RequestParam(defaultValue = "-1") int status,
                       @CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            int beginDay = DateHandler.getDay(-2);
            int endDay = DateHandler.getDay();
            Pages<GroupInfo> pages = groupService.getList(start,length,groupName,id,status,beginDay,endDay);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsTotal",pages.getTotal());
            result.put("recordsFiltered",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    @RequestMapping("/user/index")
    public String userIndex(Model model,
                            @RequestParam(defaultValue = "0") long groupId){
        model.addAttribute("groupId",groupId);
        return "group/groupUserList";
    }

    @ResponseBody
    @RequestMapping("/user/list")
    public String userList(@RequestParam(defaultValue = "0") int start,
                       @RequestParam(defaultValue = "10")int length,
                       @RequestParam(defaultValue = "")String mobile,
                       @RequestParam(defaultValue = "")String nickName,
                       @RequestParam(defaultValue = "0")long uid,
                       @RequestParam(defaultValue = "-1") int status,
                       @RequestParam(defaultValue = "-1") int userStatus,
                       @RequestParam(defaultValue = "0") int groupId,
                       @CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<GroupUserInfo> pages = groupService.userList(groupId,start,length,nickName,mobile,uid,status,userStatus);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsTotal",pages.getTotal());
            result.put("recordsFiltered",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    @ResponseBody
    @RequestMapping("/update/status")
    public String updateStatus(@RequestParam(defaultValue = "0") int status,long id){
        Map<String,Object> result = ResultUtil.result();
//        int i = groupService.updateStatus(id,status);
//        if(i<=0){
//            result.put("code",600);
//            result.put("msg","修改失败");
//        }
        if(status==1){
            String data = HttpUtils.post("http://172.24.194.56:8082/manager/group/freeze", MapHandler.initMap("id",id+"","status",status+""));
            JSONObject jsonObject = JSONObject.parseObject(data);
            if(!jsonObject.getString("code").equals("0")){
                throw new RuntimeException(jsonObject.getString("msg"));
            }
        }else{
            String data = HttpUtils.post("http://172.24.194.56:8082/manager/group/unfreeze", MapHandler.initMap("id",id+"","status",status+""));
            JSONObject jsonObject = JSONObject.parseObject(data);
            if(!jsonObject.getString("code").equals("0")){
                throw new RuntimeException(jsonObject.getString("msg"));
            }
        }
//        jedisClient.del("group:info:"+id);
        return ResultUtil.toJSON(result);
    }
    @ResponseBody
    @RequestMapping("/del")
    public String del(long id){
        Map<String,Object> result = ResultUtil.result();
        int row = groupService.deleteGroupInfo(id);
        if (row > 0) {
            groupService.deleteMembers(id);
        }else{
            result.put("code",600);
            result.put("msg","修改失败");
        }
        List<String> ids = jedisClient.getList("group:members:"+id);
        jedisClient.del("group:info:"+id);
        jedisClient.del("group:members:"+id);
        jedisClient.del("group:assistant:"+id);
        if(Common.isNotEmpty(ids)){
            if(ids.size()>0){
                ids.forEach(o -> {
                    if (jedisClient.exists("user:group:"+o)) {
                        jedisClient.mapRemove("user:group:"+o,id+"");
                    }
                });
            }
        }

        return ResultUtil.toJSON(result);
    }
    @ResponseBody
    @RequestMapping("/update/name")
    public String updateName(long id,String name){
        int i = groupService.updateName(id,name);
        Map<String,Object> result = ResultUtil.result();
        if(i<=0){
            result.put("code",600);
            result.put("msg","修改失败");
        }
        jedisClient.del("group:info:"+id);
        return ResultUtil.toJSON(result);
    }
    @ResponseBody
    @RequestMapping("/user/del")
    public String greopUserDel(long uid,long groupId){
        int i = groupService.greopUserDel(uid,groupId);
        Map<String,Object> result = ResultUtil.result();
        if(i<=0){
            result.put("code",600);
            result.put("msg","修改失败");
        }
        jedisClient.del("group:info:"+groupId);
//        jedisClient.del("group:members:"+groupId);
        jedisClient.zrem("group:members:"+groupId,uid+"");
        return ResultUtil.toJSON(result);
    }
    @ResponseBody
    @RequestMapping("/update/user/number")
    public String updateUserNumber(long id,int number){
        int i = groupService.updateUserNumber(id,number);
        Map<String,Object> result = ResultUtil.result();
        if(i<=0){
            result.put("code",600);
            result.put("msg","修改失败");
        }
        jedisClient.del("group:info:"+id);
        return ResultUtil.toJSON(result);
    }
}
